package com.ruoyi.web.controller.room;

import com.cdtu.room.domain.Reservation;
import com.cdtu.room.domain.Room;
import com.cdtu.room.dto.ReservationDto;
import com.cdtu.room.service.IReservationService;
import com.cdtu.room.service.IRoomService;
import com.cdtu.room.vo.ReservationVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author hcc
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/room/reservation")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;
    @Autowired
    private IRoomService roomService;
    /**
     * 查询预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:export')")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:query')")
    @GetMapping(value = "/{reservationId}")
    public AjaxResult getInfo(@PathVariable("reservationId") Long reservationId)
    {
        return success(reservationService.selectReservationByReservationId(reservationId));
    }

    /**
     * 新增预约信息
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation)
    {
        // 校验会议室容量是否足够
        Room room = roomService.selectRoomByRoomId(reservation.getRoomId());
        if (room.getCapacity() < reservation.getNumber()){
            return error("会议参与人员数量超过最大容纳数量!");
        }
        // 校验预约时间段是否冲突
        List<Reservation> reservationList = reservationService.checkByTime(reservation);
        if (reservationList.size() > 0){
            return error("预约时间段冲突，请更换时间段或者会议室!");
        }
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation)
    {
        if (reservation.getRoomId() != null){
            // 校验会议室容量是否足够
            Room room = roomService.selectRoomByRoomId(reservation.getRoomId());
            if (room.getCapacity() < reservation.getNumber()){
                return error("会议参与人员数量超过最大容纳数量!");
            }
        }
        // 校验预约时间段是否冲突
        List<Reservation> reservationList = reservationService.checkByTime(reservation);
        if (reservationList.size() > 0){
            return error("预约时间段冲突，请更换时间段或者会议室!");
        }
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reservationIds}")
    public AjaxResult remove(@PathVariable Long[] reservationIds)
    {
        return toAjax(reservationService.deleteReservationByReservationIds(reservationIds));
    }

    /**
     * 查询预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('room:reservation:list')")
    @GetMapping("/getList")
    public TableDataInfo getList(ReservationVo reservationVo)
    {
        startPage();
        List<ReservationDto> list = reservationService.getReservationList(reservationVo);
        return getDataTable(list);
    }
}
